<template>
	<view>
		<!-- 搜索框 -->
		<search @searchClickFn='searchClickFn'></search>
		<!-- 分类导航 -->
		<view class="cate_container">
			<!-- 左侧一级分类 -->
			<scroll-view class="cate_left" scroll-y="true" :style="{height: wh+'px'}">
				<view class="cate_left_item" :class="{'active': active == item.cat_id}"
					@click="selectCate(item.cat_id,index)" v-for="(item,index) in cateList" :key="index">
					{{item.cat_name}}
				</view>
			</scroll-view>
			<!-- 右侧一级分类 -->
			<scroll-view class="cate_right" scroll-y :style="{height: wh+'px'}" :scroll-top="scrollTop">
				<view class="cate_right_item" v-for="(item,index) in cateList2" :key="index">
					<view class="cate_right_item_title">
						/{{item.cat_name}}/
					</view>
					<view class="cate_right_item_content">
						<view @click="gotoGoodsList(item2)" class="cate_right_item_content_list" v-for="(item2,index2) in item.children" :key="index2">
							<image :src="item2.cat_icon" mode="widthFix"></image>
							<text>{{item2.cat_name}}</text>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>

	</view>
</template>

<script>
	import badgeMix from '@/mixins/tabbar-badge.js'
	export default {
		mixins: [badgeMix],
		data() {
			return {
				wh: 0, //屏幕高度
				cateList: [], //分类数据
				cateList2: [], //一级分类详情
				active: 0, //选中的一级分类
				scrollTop:0
			}
		},
		methods: {
			// 获取分类数据
			async getCateList() {
				let {
					data
				} = await uni.$http.get('/categories')
				if (data.meta.status !== 200) return uni.$showMsg()
				this.active = data.message[0].cat_id || 0
				this.cateList2 = data.message[0].children || []
				this.cateList = data.message
			},
			// 选中一级分类
			selectCate(item, index) {
				this.active = item
				this.cateList2 = this.cateList[index].children || []
				this.scrollTop = Math.random()
			},
			// 点击分类详情跳转到商品列表页
			gotoGoodsList(item){
				 uni.navigateTo({
				 	url:`../../subpkg/goods_list/goods_list?cid=${item.cat_id}`
				 })
			},
			//点击搜索框
			searchClickFn(){
				uni.navigateTo({
					url:`../../subpkg/search/search`
				})
			}
		},
		onLoad() {
			// 获取页面可用高度
			const sysInfo = uni.getSystemInfoSync()
			// 为wh赋值(windowHeight, 可使用窗口高度)
			this.wh = sysInfo.windowHeight - 50
			// 获取分类数据
			this.getCateList()
		}
	}
</script>

<style lang="scss">
	.cate_container {
		display: flex;

		.cate_left {
			width: 240rpx;

			.cate_left_item {
				display: flex;
				justify-content: center;
				align-items: center;
				height: 120rpx;
				background-color: #efefef;
				font-size: 24rpx;

				// 选中样式
				&.active {
					display: flex;
					justify-content: space-between;
					background-color: #ffffff;

					// 左边红线
					&::before {
						content: '';
						width: 6rpx;
						height: 60rpx;
						background-color: #c00000;
					}

					// 右边白线和背景相同
					&::after {
						content: '';
						width: 6rpx;
						height: 60rpx;
						background-color: #ffffff;
					}
				}

			}
		}

		.cate_right {
			background-color: white;

			.cate_right_item {
				.cate_right_item_title {
					display: flex;
					justify-content: center;
					font-weight: bold;
					font-size: 24rpx;
					padding: 20rpx 0;
				}

				.cate_right_item_content {
					display: flex;
					flex-wrap: wrap;

					.cate_right_item_content_list {
						display: flex;
						flex-direction: column;
						justify-content: center;
						align-items: center;
						width: 33.33%;
						padding: 20rpx;
						box-sizing: border-box;

						image {
							width: 100%;
						}

						text {
							font-size: 24rpx;
						}
					}
				}
			}

		}
	}
</style>